
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>9.1 线程池 · C++并发编程实战（第2版）</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="9.2-chinese.html" />
    
    
    <link rel="prev" href="9.0-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Praise_for_the_first_edition.html">
            
                <a href="../Praise_for_the_first_edition.html">
            
                    
                    第一版的赞许
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../preface-chinese.html">
            
                <a href="../preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../acknowledgments-chinese.html">
            
                <a href="../acknowledgments-chinese.html">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../about_this_book-chinese.html">
            
                <a href="../about_this_book-chinese.html">
            
                    
                    关于本书
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../about_the_author-chinese.html">
            
                <a href="../about_the_author-chinese.html">
            
                    
                    关于作者
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../about_cover_illustration-chinese.html">
            
                <a href="../about_cover_illustration-chinese.html">
            
                    
                    关于封面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 你好，并发世界
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 何谓并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 为什么使用并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 并发和多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 开始入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 线程的基本操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 传递参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 转移所有权
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 确定线程数量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 线程标识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 共享数据
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 共享数据的问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 使用互斥量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 保护共享数据的方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 同步操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 等待事件或条件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 限时等待
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 简化代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 内存模型和原子操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 内存模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 原子操作和原子类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 同步操作和强制排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 设计基于锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 并发设计的意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 基于锁的并发数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 设计更加复杂的数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 设计无锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 定义和意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 无锁数据结构的例子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 设计无锁数据结构的指导建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 并发设计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 线程间划分工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 并发代码的性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 为多线程性能设计数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 设计并发代码的注意事项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 在实践中设计并发代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.6" data-path="../chapter8/8.6-chinese.html">
            
                <a href="../chapter8/8.6-chinese.html">
            
                    
                    8.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="9.0-chinese.html">
            
                <a href="9.0-chinese.html">
            
                    
                    第9章 高级线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.16.1" data-path="9.1-chinese.html">
            
                <a href="9.1-chinese.html">
            
                    
                    9.1 线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="9.2-chinese.html">
            
                <a href="9.2-chinese.html">
            
                    
                    9.2 中断线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="9.3-chinese.html">
            
                <a href="9.3-chinese.html">
            
                    
                    9.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter10/10.0-chinese.html">
            
                <a href="../chapter10/10.0-chinese.html">
            
                    
                    第10章 并行算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter10/10.1-chinese.html">
            
                <a href="../chapter10/10.1-chinese.html">
            
                    
                    10.1 并行化标准库算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter10/10.2-chinese.html">
            
                <a href="../chapter10/10.2-chinese.html">
            
                    
                    10.2 执行策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter10/10.3-chinese.html">
            
                <a href="../chapter10/10.3-chinese.html">
            
                    
                    10.3 C++标准库中的并行算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter10/10.4-chinese.html">
            
                <a href="../chapter10/10.4-chinese.html">
            
                    
                    10.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 测试和调试多线程应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 与并发相关的Bug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 定位并发Bug的技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../appendix_A/A.0-chinese.html">
            
                <a href="../appendix_A/A.0-chinese.html">
            
                    
                    附录A C++11特性简明参考(部分)
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../appendix_A/A.1-chinese.html">
            
                <a href="../appendix_A/A.1-chinese.html">
            
                    
                    A.1 右值引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.2" data-path="../appendix_A/A.2-chinese.html">
            
                <a href="../appendix_A/A.2-chinese.html">
            
                    
                    A.2 删除函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.3" data-path="../appendix_A/A.3-chinese.html">
            
                <a href="../appendix_A/A.3-chinese.html">
            
                    
                    A.3 默认函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.4" data-path="../appendix_A/A.4-chinese.html">
            
                <a href="../appendix_A/A.4-chinese.html">
            
                    
                    A.4 常量表达式函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.5" data-path="../appendix_A/A.5-chinese.html">
            
                <a href="../appendix_A/A.5-chinese.html">
            
                    
                    A.5 Lambda函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.6" data-path="../appendix_A/A.6-chinese.html">
            
                <a href="../appendix_A/A.6-chinese.html">
            
                    
                    A.6 变参模板
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.7" data-path="../appendix_A/A.7-chinese.html">
            
                <a href="../appendix_A/A.7-chinese.html">
            
                    
                    A.7 自动推导变量类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.8" data-path="../appendix_A/A.8-chinese.html">
            
                <a href="../appendix_A/A.8-chinese.html">
            
                    
                    A.8 线程本地变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.9" data-path="../appendix_A/A.9-chinese.html">
            
                <a href="../appendix_A/A.9-chinese.html">
            
                    
                    A.9 模板类参数的推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.10" data-path="../appendix_A/A.10-chinese.html">
            
                <a href="../appendix_A/A.10-chinese.html">
            
                    
                    A.10 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../appendix_B/B.0-chinese.html">
            
                <a href="../appendix_B/B.0-chinese.html">
            
                    
                    附录B 并发库简要对比
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../appendix_C/C.0-chinese.html">
            
                <a href="../appendix_C/C.0-chinese.html">
            
                    
                    附录C 消息传递框架与完整的ATM示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../appendix_D/D.0-chinese.html">
            
                <a href="../appendix_D/D.0-chinese.html">
            
                    
                    附录D C++线程类库参考
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.22.1" data-path="../appendix_D/D.1-chinese.html">
            
                <a href="../appendix_D/D.1-chinese.html">
            
                    
                    D.1 chrono头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.2" data-path="../appendix_D/D.2-chinese.html">
            
                <a href="../appendix_D/D.2-chinese.html">
            
                    
                    D.2 condition_variable头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.3" data-path="../appendix_D/D.3-chinese.html">
            
                <a href="../appendix_D/D.3-chinese.html">
            
                    
                    D.3 atomic头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.4" data-path="../appendix_D/D.4-chinese.html">
            
                <a href="../appendix_D/D.4-chinese.html">
            
                    
                    D.4 future头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.5" data-path="../appendix_D/D.5-chinese.html">
            
                <a href="../appendix_D/D.5-chinese.html">
            
                    
                    D.5 mutex头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.6" data-path="../appendix_D/D.6-chinese.html">
            
                <a href="../appendix_D/D.6-chinese.html">
            
                    
                    D.6 ratio头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.7" data-path="../appendix_D/D.7-chinese.html">
            
                <a href="../appendix_D/D.7-chinese.html">
            
                    
                    D.7 thread头文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >9.1 线程池</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="91-&#x7EBF;&#x7A0B;&#x6C60;">9.1 &#x7EBF;&#x7A0B;&#x6C60;</h1>
<p>&#x5F88;&#x591A;&#x516C;&#x53F8;&#x91CC;&#xFF0C;&#x96C7;&#x5458;&#x901A;&#x5E38;&#x4F1A;&#x5728;&#x529E;&#x516C;&#x5BA4;&#x5EA6;&#x8FC7;&#x4ED6;&#x4EEC;&#x7684;&#x529E;&#x516C;&#x65F6;&#x5149;(&#x5076;&#x5C14;&#x4E5F;&#x4F1A;&#x5916;&#x51FA;&#x8BBF;&#x95EE;&#x5BA2;&#x6237;&#x6216;&#x4F9B;&#x5E94;&#x5546;)&#xFF0C;&#x6216;&#x662F;&#x53C2;&#x52A0;&#x8D38;&#x6613;&#x5C55;&#x4F1A;&#x3002;&#x867D;&#x7136;&#x5916;&#x51FA;&#x53EF;&#x80FD;&#x5F88;&#x6709;&#x5FC5;&#x8981;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5F88;&#x591A;&#x4EBA;&#x4E00;&#x8D77;&#x53BB;&#xFF0C;&#x4E0D;&#x8FC7;&#x5BF9;&#x4E8E;&#x4E00;&#x4E9B;&#x7279;&#x522B;&#x7684;&#x96C7;&#x5458;&#x6765;&#x8BF4;&#xFF0C;&#x4E00;&#x8D9F;&#x53EF;&#x80FD;&#x5C31;&#x662F;&#x51E0;&#x4E2A;&#x6708;&#xFF0C;&#x751A;&#x81F3;&#x662F;&#x51E0;&#x5E74;&#x3002;&#x516C;&#x53F8;&#x4E0D;&#x53EF;&#x80FD;&#x7ED9;&#x6BCF;&#x4E2A;&#x96C7;&#x5458;&#x90FD;&#x914D;&#x4E00;&#x8F86;&#x8F66;&#xFF0C;&#x4E0D;&#x8FC7;&#x516C;&#x53F8;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x4E00;&#x4E9B;&#x5171;&#x7528;&#x8F66;&#x8F86;&#x3002;&#x8FD9;&#x6837;&#x5C31;&#x4F1A;&#x6709;&#x4E00;&#x5B9A;&#x6570;&#x91CF;&#x8F66;&#xFF0C;&#x6765;&#x8BA9;&#x6240;&#x6709;&#x96C7;&#x5458;&#x4F7F;&#x7528;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x5458;&#x5DE5;&#x8981;&#x53BB;&#x5F02;&#x5730;&#x65C5;&#x6E38;&#x65F6;&#xFF0C;&#x4ED6;&#x5C31;&#x53EF;&#x4EE5;&#x4ECE;&#x5171;&#x7528;&#x8F66;&#x8F86;&#x4E2D;&#x9884;&#x5B9A;&#x4E00;&#x8F86;&#xFF0C;&#x5E76;&#x5728;&#x8FD4;&#x56DE;&#x516C;&#x53F8;&#x7684;&#x65F6;&#x5019;&#x5C06;&#x8F66;&#x4EA4;&#x8FD8;&#x3002;&#x5982;&#x679C;&#x67D0;&#x5929;&#x6CA1;&#x6709;&#x95F2;&#x7F6E;&#x7684;&#x5171;&#x7528;&#x8F66;&#x8F86;&#xFF0C;&#x96C7;&#x5458;&#x5C31;&#x5F97;&#x4E0D;&#x5EF6;&#x540E;&#x5176;&#x65C5;&#x7A0B;&#x4E86;&#x3002;</p>
<p>&#x7EBF;&#x7A0B;&#x6C60;&#x5C31;&#x662F;&#x7C7B;&#x4F3C;&#x7684;&#x8C03;&#x5EA6;&#x673A;&#x5236;&#x3002;&#x5927;&#x591A;&#x6570;&#x7CFB;&#x7EDF;&#x4E2D;&#xFF0C;&#x5C06;&#x6BCF;&#x4E2A;&#x4EFB;&#x52A1;&#x6307;&#x5B9A;&#x7ED9;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x662F;&#x4E0D;&#x5207;&#x5B9E;&#x9645;&#x7684;&#xFF0C;&#x4E0D;&#x8FC7;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x5E76;&#x53D1;&#x6027;&#xFF0C;&#x8FDB;&#x884C;&#x5E76;&#x53D1;&#x6267;&#x884C;&#x3002;&#x7EBF;&#x7A0B;&#x6C60;&#x63D0;&#x4F9B;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5C06;&#x63D0;&#x4EA4;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#x5E76;&#x53D1;&#x6267;&#x884C;&#xFF0C;&#x63D0;&#x4EA4;&#x7684;&#x4EFB;&#x52A1;&#x5C06;&#x4F1A;&#x6302;&#x5728;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x3002;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x4F1A;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#xFF0C;&#x5F53;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x518D;&#x4ECE;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x4E0B;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x3002;</p>
<p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6C60;&#x65F6;&#xFF0C;&#x4F1A;&#x9047;&#x5230;&#x51E0;&#x4E2A;&#x5173;&#x952E;&#x6027;&#x7684;&#x8BBE;&#x8BA1;&#x95EE;&#x9898;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x53EF;&#x4F7F;&#x7528;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#xFF0C;&#x9AD8;&#x6548;&#x7684;&#x4EFB;&#x52A1;&#x5206;&#x914D;&#x65B9;&#x5F0F;&#xFF0C;&#x4EE5;&#x53CA;&#x662F;&#x5426;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x3002;</p>
<p>&#x672C;&#x8282;&#x5C06;&#x770B;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#x662F;&#x5982;&#x4F55;&#x89E3;&#x51B3;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#x7684;&#xFF0C;&#x4ECE;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x5F00;&#x59CB;&#x5427;&#xFF01;</p>
<h2 id="911-&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;">9.1.1 &#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</h2>
<p>&#x4F5C;&#x4E3A;&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x5176;&#x62E5;&#x6709;&#x56FA;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;(&#x901A;&#x5E38;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#x4E0E;<code>std::thread::hardware_concurrency()</code>&#x76F8;&#x540C;)&#x3002;&#x5DE5;&#x4F5C;&#x9700;&#x8981;&#x5B8C;&#x6210;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x5C06;&#x4EFB;&#x52A1;&#x6302;&#x5728;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x3002;&#x6BCF;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x90FD;&#x4F1A;&#x4ECE;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#x518D;&#x56DE;&#x6765;&#x83B7;&#x53D6;&#x65B0;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7EBF;&#x7A0B;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x5BF9;&#x5E94;&#x4EFB;&#x52A1;&#x4E86;&#x3002;&#x5982;&#x679C;&#x9700;&#x8981;&#x7B49;&#x5F85;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x5BF9;&#x540C;&#x6B65;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x6E05;&#x5355;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x5C55;&#x793A;&#x4E86;&#x4E00;&#x4E2A;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.1 &#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> thread_pool
{
  <span class="hljs-built_in">std</span>::<span class="hljs-keyword">atomic_bool</span> done;
  thread_safe_queue&lt;<span class="hljs-built_in">std</span>::function&lt;<span class="hljs-keyword">void</span>()&gt; &gt; work_queue;  <span class="hljs-comment">// 1</span>
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">std</span>::thread&gt; threads;  <span class="hljs-comment">// 2</span>
  join_threads joiner;  <span class="hljs-comment">// 3</span>

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">worker_thread</span><span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">while</span>(!done)  <span class="hljs-comment">// 4</span>
    {
      <span class="hljs-built_in">std</span>::function&lt;<span class="hljs-keyword">void</span>()&gt; task;
      <span class="hljs-keyword">if</span>(work_queue.try_pop(task))  <span class="hljs-comment">// 5</span>
      {
        task();  <span class="hljs-comment">// 6</span>
      }
      <span class="hljs-keyword">else</span>
      {
        <span class="hljs-built_in">std</span>::this_thread::yield();  <span class="hljs-comment">// 7</span>
      }
    }
  }

<span class="hljs-keyword">public</span>:
  thread_pool():
    done(<span class="hljs-literal">false</span>),joiner(threads)
  {
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">const</span> thread_count=<span class="hljs-built_in">std</span>::thread::hardware_concurrency();  <span class="hljs-comment">// 8</span>

    <span class="hljs-keyword">try</span>
    {
      <span class="hljs-keyword">for</span>(<span class="hljs-keyword">unsigned</span> i=<span class="hljs-number">0</span>;i&lt;thread_count;++i)
      {
        threads.push_back( 
          <span class="hljs-built_in">std</span>::thread(&amp;thread_pool::worker_thread,<span class="hljs-keyword">this</span>));  <span class="hljs-comment">// 9</span>
      }
    }
    <span class="hljs-keyword">catch</span>(...)
    {
      done=<span class="hljs-literal">true</span>;  <span class="hljs-comment">// 10</span>
      <span class="hljs-keyword">throw</span>;
    }
  }

  ~thread_pool()
  {
    done=<span class="hljs-literal">true</span>;  <span class="hljs-comment">// 11</span>
  }

  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> FunctionType&gt;
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">submit</span><span class="hljs-params">(FunctionType f)</span>
  </span>{
    work_queue.push(<span class="hljs-built_in">std</span>::function&lt;<span class="hljs-keyword">void</span>()&gt;(f));  <span class="hljs-comment">// 12</span>
  }
};
</code></pre>
<p>&#x5B9E;&#x73B0;&#x4E2D;&#x6709;&#x4E00;&#x7EC4;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x2461;&#xFF0C;&#x5E76;&#x4E14;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x961F;&#x5217;(&#x89C1;&#x7B2C;6&#x7AE0;)&#x2460;&#x6765;&#x7BA1;&#x7406;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7528;&#x6237;&#x4E0D;&#x7528;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#xFF0C;&#x5E76;&#x4E14;&#x4EFB;&#x52A1;&#x4E0D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x503C;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>std::function&lt;void()&gt;</code>&#x5BF9;&#x4EFB;&#x52A1;&#x8FDB;&#x884C;&#x5C01;&#x88C5;&#x3002;submit()&#x4F1A;&#x5C06;&#x51FD;&#x6570;&#x6216;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x5305;&#x88C5;&#x6210;&#x4E00;&#x4E2A;<code>std::function&lt;void()&gt;</code>&#x5B9E;&#x4F8B;&#xFF0C;&#x5C06;&#x5176;&#x63A8;&#x5165;&#x961F;&#x5217;&#x4E2D;&#x246B;&#x3002;</p>
<p>&#x7EBF;&#x7A0B;&#x59CB;&#x4E8E;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF1A;&#x4F7F;&#x7528;<code>std::thread::hardware_concurrency()</code>&#x6765;&#x83B7;&#x53D6;&#x786C;&#x4EF6;&#x652F;&#x6301;&#x591A;&#x5C11;&#x4E2A;&#x5E76;&#x53D1;&#x7EBF;&#x7A0B;&#x2467;&#xFF0C;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x4F1A;&#x5728;worker_thread()&#x6210;&#x5458;&#x51FD;&#x6570;&#x4E2D;&#x6267;&#x884C;&#x2468;&#x3002;</p>
<p>&#x5F53;&#x6709;&#x5F02;&#x5E38;&#x629B;&#x51FA;&#x65F6;&#xFF0C;&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x5C31;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x4FDD;&#x8BC1;&#x4EFB;&#x4F55;&#x5DF2;&#x542F;&#x52A8;&#x7684;&#x7EBF;&#x7A0B;&#x90FD;&#x80FD;&#x505C;&#x6B62;&#x3002;&#x6709;&#x5F02;&#x5E38;&#x629B;&#x51FA;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;<em>try-catch</em>&#x6765;&#x8BBE;&#x7F6E;done&#x6807;&#x5FD7;&#x2469;&#xFF0C;&#x8FD8;&#x6709;join_threads&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;(&#x6765;&#x81EA;&#x4E8E;&#x7B2C;8&#x7AE0;)&#x2462;&#x6765;&#x6C47;&#x805A;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x3002;&#x5F53;&#x7136;&#x4E5F;&#x9700;&#x8981;&#x6790;&#x6784;&#x51FD;&#x6570;&#xFF1A;&#x4EC5;&#x8BBE;&#x7F6E;done&#x6807;&#x5FD7;&#x246A;&#xFF0C;&#x5E76;&#x4E14;join_threads&#x786E;&#x4FDD;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x9500;&#x6BC1;&#x524D;&#x5168;&#x90E8;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x3002;&#x6CE8;&#x610F;&#x6210;&#x5458;&#x58F0;&#x660E;&#x7684;&#x987A;&#x5E8F;&#x5F88;&#x91CD;&#x8981;&#xFF1A;done&#x6807;&#x5FD7;&#x548C;worker_queue&#x5FC5;&#x987B;&#x5728;threads&#x6570;&#x7EC4;&#x4E4B;&#x524D;&#x58F0;&#x660E;&#xFF0C;&#x800C;&#x6570;&#x636E;&#x5FC5;&#x987B;&#x5728;joiner&#x524D;&#x58F0;&#x660E;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x80FD;&#x786E;&#x4FDD;&#x6210;&#x5458;&#x4EE5;&#x6B63;&#x786E;&#x7684;&#x987A;&#x5E8F;&#x9500;&#x6BC1;&#x3002;</p>
<p>worker_thread&#x51FD;&#x6570;&#x5F88;&#x7B80;&#x5355;&#xFF1A;&#x4ECE;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#x2464;&#xFF0C;&#x4EE5;&#x53CA;&#x540C;&#x65F6;&#x6267;&#x884C;&#x8FD9;&#x4E9B;&#x4EFB;&#x52A1;&#x2465;&#xFF0C;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x76F4;&#x5230;&#x8BBE;&#x7F6E;done&#x6807;&#x5FD7;&#x2463;&#x3002;&#x5982;&#x679C;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x6CA1;&#x6709;&#x4EFB;&#x52A1;&#xFF0C;&#x51FD;&#x6570;&#x4F1A;&#x8C03;&#x7528;<code>std::this_thread::yield()</code>&#x8BA9;&#x7EBF;&#x7A0B;&#x4F11;&#x606F;&#x2466;&#xFF0C;&#x5E76;&#x4E14;&#x7ED9;&#x4E88;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5411;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x63A8;&#x9001;&#x4EFB;&#x52A1;&#x7684;&#x673A;&#x4F1A;&#x3002;</p>
<p>&#x8FD9;&#x6837;&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x5C31;&#x5B8C;&#x6210;&#x4E86;&#xFF0C;&#x7279;&#x522B;&#x662F;&#x4EFB;&#x52A1;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6216;&#x9700;&#x8981;&#x6267;&#x884C;&#x963B;&#x585E;&#x64CD;&#x4F5C;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x5F88;&#x591A;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x662F;&#x4E0D;&#x591F;&#x7528;&#x7684;&#xFF0C;&#x5176;&#x4ED6;&#x60C5;&#x51B5;&#x4F7F;&#x7528;&#x8FD9;&#x6837;&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#x95EE;&#x9898;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x6B7B;&#x9501;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x5728;&#x7B80;&#x5355;&#x4F8B;&#x5B50;&#x4E2D;&#x4F7F;&#x7528;<code>std::async</code>&#x80FD;&#x63D0;&#x4F9B;&#x66F4;&#x597D;&#x7684;&#x529F;&#x80FD;(&#x5982;&#x7B2C;8&#x7AE0;&#x4E2D;&#x7684;&#x4F8B;&#x5B50;)&#x3002;</p>
<p>&#x672C;&#x7AE0;&#x5C06;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#x66F4;&#x52A0;&#x590D;&#x6742;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x5B9E;&#x73B0;&#xFF0C;&#x901A;&#x8FC7;&#x6DFB;&#x52A0;&#x7279;&#x6027;&#x6EE1;&#x8DB3;&#x7528;&#x6237;&#x9700;&#x6C42;&#xFF0C;&#x6216;&#x51CF;&#x5C11;&#x95EE;&#x9898;&#x53D1;&#x751F;&#x7684;&#x51E0;&#x7387;&#x3002;</p>
<h2 id="912-&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;">9.1.2 &#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;</h2>
<p>&#x7B2C;8&#x7AE0;&#x4E2D;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x7EBF;&#x7A0B;&#x95F4;&#x7684;&#x4EFB;&#x52A1;&#x5212;&#x5206;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x4EE3;&#x7801;&#x4F1A;&#x663E;&#x5F0F;&#x751F;&#x6210;&#x65B0;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E3B;&#x7EBF;&#x7A0B;&#x901A;&#x5E38;&#x662F;&#x7B49;&#x5F85;&#x65B0;&#x7EBF;&#x7A0B;&#x5728;&#x8FD4;&#x56DE;&#x8C03;&#x7528;&#x4E4B;&#x540E;&#x7ED3;&#x675F;&#xFF0C;&#x786E;&#x4FDD;&#x6240;&#x6709;&#x4EFB;&#x52A1;&#x90FD;&#x5B8C;&#x6210;&#x3002;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x5C31;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x63D0;&#x4EA4;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#xFF0C;&#x800C;&#x975E;&#x76F4;&#x63A5;&#x63D0;&#x4EA4;&#x7ED9;&#x5355;&#x4E2A;&#x7EBF;&#x7A0B;&#x3002;&#x4E0E;&#x57FA;&#x4E8E;<code>std::async</code>&#x7684;&#x65B9;&#x6CD5;&#x7C7B;&#x4F3C;&#xFF0C;&#x4F7F;&#x7528;&#x4EE3;&#x7801;9.1&#x4E2D;&#x7684;&#x7B80;&#x5355;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x4F7F;&#x7528;&#x7B2C;4&#x7AE0;&#x4E2D;&#x63D0;&#x5230;&#x7684;&#x5DE5;&#x5177;&#xFF1A;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x548C;future&#x3002;&#x867D;&#x7136;&#x4F1A;&#x589E;&#x52A0;&#x4EE3;&#x7801;&#x7684;&#x590D;&#x6742;&#x5EA6;&#xFF0C;&#x4E0D;&#x8FC7;&#x8981;&#x6BD4;&#x76F4;&#x63A5;&#x5BF9;&#x4EFB;&#x52A1;&#x8FDB;&#x884C;&#x7B49;&#x5F85;&#x597D;&#x5F88;&#x591A;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x589E;&#x52A0;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x590D;&#x6742;&#x5EA6;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x3002;&#x4F7F;&#x7528;submit()&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x5BF9;&#x4EFB;&#x52A1;&#x63CF;&#x8FF0;&#x7684;&#x53E5;&#x67C4;&#xFF0C;&#x53EF;&#x7528;&#x6765;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x7684;&#x5B8C;&#x6210;&#x3002;&#x4EFB;&#x52A1;&#x53E5;&#x67C4;&#x4F1A;&#x7528;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x6216;future&#x8FDB;&#x884C;&#x5305;&#x88C5;&#xFF0C;&#x4ECE;&#x800C;&#x7B80;&#x5316;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x60C5;&#x51B5;&#x662F;&#xFF0C;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5230;&#x4E3B;&#x7EBF;&#x7A0B;&#x4E0A;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;&#x672C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x9700;&#x8981;&#x7528;future&#x5BF9;&#x6700;&#x7EC8;&#x7684;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x8F6C;&#x79FB;&#x3002;&#x4EE3;&#x7801;9.2&#x5C55;&#x793A;&#x4E86;&#x5BF9;&#x7B80;&#x5355;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4FEE;&#x6539;&#xFF0C;&#x901A;&#x8FC7;&#x4FEE;&#x6539;&#x5C31;&#x80FD;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#xFF0C;&#x4EE5;&#x53CA;&#x5728;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7ED3;&#x679C;&#x5230;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x4E2D;&#x53BB;&#xFF0C;&#x4E0D;&#x8FC7;<code>std::packaged_task&lt;&gt;</code>&#x5B9E;&#x4F8B;&#x662F;&#x4E0D;&#x53EF;&#x62F7;&#x8D1D;&#x7684;&#xFF0C;&#x4EC5;&#x53EF;&#x79FB;&#x52A8;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x518D;&#x4F7F;&#x7528;<code>std::function&lt;&gt;</code>&#x6765;&#x5B9E;&#x73B0;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x56E0;&#x4E3A;<code>std::function&lt;&gt;</code>&#x9700;&#x8981;&#x5B58;&#x50A8;&#x53EF;&#x590D;&#x5236;&#x6784;&#x9020;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x3002;&#x5305;&#x88C5;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x5904;&#x7406;&#x53EF;&#x79FB;&#x52A8;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x51FD;&#x6570;&#x64CD;&#x4F5C;&#x7B26;&#x7684;&#x7C7B;&#x578B;&#x64E6;&#x9664;&#x7C7B;&#x3002;&#x53EA;&#x9700;&#x8981;&#x5904;&#x7406;&#x6CA1;&#x6709;&#x5165;&#x53C2;&#x7684;&#x51FD;&#x6570;&#x548C;&#x65E0;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x5373;&#x53EF;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x865A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.2 &#x53EF;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> function_wrapper
{
  <span class="hljs-keyword">struct</span> impl_base {
    <span class="hljs-function"><span class="hljs-keyword">virtual</span> <span class="hljs-keyword">void</span> <span class="hljs-title">call</span><span class="hljs-params">()</span></span>=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">virtual</span> ~impl_base() {}
  };

  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">unique_ptr</span>&lt;impl_base&gt; impl;
  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> F&gt;
  <span class="hljs-keyword">struct</span> impl_type: impl_base
  {
    F f;
    impl_type(F&amp;&amp; f_): f(<span class="hljs-built_in">std</span>::move(f_)) {}
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">call</span><span class="hljs-params">()</span> </span>{ f(); }
  };
<span class="hljs-keyword">public</span>:
  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> F&gt;
  function_wrapper(F&amp;&amp; f):
    impl(<span class="hljs-keyword">new</span> impl_type&lt;F&gt;(<span class="hljs-built_in">std</span>::move(f)))
  {}

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">()</span> </span>{ impl-&gt;call(); }

  function_wrapper() = <span class="hljs-keyword">default</span>;

  function_wrapper(function_wrapper&amp;&amp; other):
    impl(<span class="hljs-built_in">std</span>::move(other.impl))
  {}

  function_wrapper&amp; <span class="hljs-keyword">operator</span>=(function_wrapper&amp;&amp; other)
  {
    impl=<span class="hljs-built_in">std</span>::move(other.impl);
    <span class="hljs-keyword">return</span> *<span class="hljs-keyword">this</span>;
  }

  function_wrapper(<span class="hljs-keyword">const</span> function_wrapper&amp;)=<span class="hljs-keyword">delete</span>;
  function_wrapper(function_wrapper&amp;)=<span class="hljs-keyword">delete</span>;
  function_wrapper&amp; <span class="hljs-keyword">operator</span>=(<span class="hljs-keyword">const</span> function_wrapper&amp;)=<span class="hljs-keyword">delete</span>;
};

<span class="hljs-keyword">class</span> thread_pool
{
  thread_safe_queue&lt;function_wrapper&gt; work_queue;  <span class="hljs-comment">// &#x4F7F;&#x7528;function_wrapper&#xFF0C;&#x800C;&#x975E;&#x4F7F;&#x7528;std::function</span>

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">worker_thread</span><span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">while</span>(!done)
    {
      function_wrapper task;
      <span class="hljs-keyword">if</span>(work_queue.try_pop(task))
      {
        task();
      }
      <span class="hljs-keyword">else</span>
      {
        <span class="hljs-built_in">std</span>::this_thread::yield();
      }
    }
  }
<span class="hljs-keyword">public</span>:
  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> FunctionType&gt;
  <span class="hljs-built_in">std</span>::future&lt;<span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::result_of&lt;FunctionType()&gt;::type&gt;  <span class="hljs-comment">// 1</span>
    submit(FunctionType f)
  {
    <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::result_of&lt;FunctionType()&gt;::type
      result_type;  <span class="hljs-comment">// 2</span>

    <span class="hljs-built_in">std</span>::packaged_task&lt;result_type()&gt; task(<span class="hljs-built_in">std</span>::move(f));  <span class="hljs-comment">// 3</span>
    <span class="hljs-built_in">std</span>::future&lt;result_type&gt; res(task.get_future());  <span class="hljs-comment">// 4</span>
    work_queue.push(<span class="hljs-built_in">std</span>::move(task));  <span class="hljs-comment">// 5</span>
    <span class="hljs-keyword">return</span> res;  <span class="hljs-comment">// 6</span>
  }
  <span class="hljs-comment">// &#x548C;&#x4E4B;&#x524D;&#x4E00;&#x6837;</span>
};
</code></pre>
<p>&#x9996;&#x5148;&#xFF0C;&#x4FEE;&#x6539;&#x7684;&#x662F;submit()&#x51FD;&#x6570;&#x2460;&#xFF0C;&#x8FD4;&#x56DE;<code>std::future&lt;&gt;</code>&#x4FDD;&#x5B58;&#x4EFB;&#x52A1;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x5141;&#x8BB8;&#x8C03;&#x7528;&#x8005;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x5B8C;&#x5168;&#x7ED3;&#x675F;&#x3002;&#x56E0;&#x4E3A;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x51FD;&#x6570;f&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x4F7F;&#x7528;<code>std::result_of&lt;&gt;</code>&#xFF1A;<code>std::result_of&lt;FunctionType()&gt;::type</code>&#x662F;FunctionType&#x7C7B;&#x578B;&#x7684;&#x5F15;&#x7528;&#x5B9E;&#x4F8B;(&#x5982;f)&#xFF0C;&#x5E76;&#x4E14;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x51FD;&#x6570;&#x4E2D;&#x53EF;&#x4EE5;&#x5BF9;result_type typedef&#x2461;&#x4F7F;&#x7528;<code>std::result_of&lt;&gt;</code>&#x3002;</p>
<p>&#x7136;&#x540E;&#xFF0C;&#x5C06;f&#x5305;&#x88C5;&#x5165;<code>std::packaged_task&lt;result_type()&gt;</code>&#x2462;&#xFF0C;&#x56E0;&#x4E3A;f&#x662F;&#x4E00;&#x4E2A;&#x65E0;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;&#x6216;&#x662F;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#xFF0C;&#x80FD;&#x591F;&#x8FD4;&#x56DE;result_type&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x5411;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x63A8;&#x9001;&#x4EFB;&#x52A1;&#x2464;&#x548C;&#x8FD4;&#x56DE;future&#x2465;&#x524D;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4ECE;<code>std::packaged_task&lt;&gt;</code>&#x4E2D;&#x83B7;&#x53D6;future&#x2463;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8981;&#x5C06;&#x4EFB;&#x52A1;&#x63A8;&#x9001;&#x5230;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x65F6;&#xFF0C;&#x53EA;&#x80FD;&#x4F7F;&#x7528;<code>std::move()</code>&#xFF0C;&#x56E0;&#x4E3A;<code>std::packaged_task&lt;&gt;</code>&#x4E0D;&#x53EF;&#x62F7;&#x8D1D;&#x3002;&#x4E3A;&#x4E86;&#x5BF9;&#x4EFB;&#x52A1;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x961F;&#x5217;&#x91CC;&#x9762;&#x5B58;&#x7684;&#x5C31;&#x662F;function_wrapper&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x975E;<code>std::function&lt;void()&gt;</code>&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x5141;&#x8BB8;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#xFF0C;&#x5E76;&#x4E14;&#x8FD4;&#x56DE;&#x4EFB;&#x52A1;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x5C55;&#x793A;&#x4E86;&#xFF0C;&#x5982;&#x4F55;&#x8BA9;parallel_accumuate&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.3 parallel_accumulate&#x4F7F;&#x7528;&#x53EF;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> Iterator,<span class="hljs-keyword">typename</span> T&gt;
<span class="hljs-function">T <span class="hljs-title">parallel_accumulate</span><span class="hljs-params">(Iterator first,Iterator last,T init)</span>
</span>{
  <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> <span class="hljs-keyword">const</span> length=<span class="hljs-built_in">std</span>::distance(first,last);

  <span class="hljs-keyword">if</span>(!length)
    <span class="hljs-keyword">return</span> init;

  <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> <span class="hljs-keyword">const</span> block_size=<span class="hljs-number">25</span>;
  <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> <span class="hljs-keyword">const</span> num_blocks=(length+block_size<span class="hljs-number">-1</span>)/block_size;  <span class="hljs-comment">// 1</span>

  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">std</span>::future&lt;T&gt; &gt; futures(num_blocks<span class="hljs-number">-1</span>);
  thread_pool pool;

  Iterator block_start=first;
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> i=<span class="hljs-number">0</span>;i&lt;(num_blocks<span class="hljs-number">-1</span>);++i)
  {
    Iterator block_end=block_start;
    <span class="hljs-built_in">std</span>::advance(block_end,block_size);
    futures[i]=pool.submit([=]{
      accumulate_block&lt;Iterator,T&gt;()(block_start,block_end);
    }); <span class="hljs-comment">// 2</span>
    block_start=block_end;
  }
  T last_result=accumulate_block&lt;Iterator,T&gt;()(block_start,last);
  T result=init;
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> i=<span class="hljs-number">0</span>;i&lt;(num_blocks<span class="hljs-number">-1</span>);++i)
  {
    result+=futures[i].get();
  }
  result += last_result;
  <span class="hljs-keyword">return</span> result;
}
</code></pre>
<p>&#x4E0E;&#x4EE3;&#x7801;8.4&#x76F8;&#x6BD4;&#xFF0C;&#x6709;&#x51E0;&#x4E2A;&#x70B9;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x5DE5;&#x4F5C;&#x91CF;&#x662F;&#x4F9D;&#x636E;&#x4F7F;&#x7528;&#x7684;&#x5757;&#x6570;(num_blocks&#x2460;)&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7EBF;&#x7A0B;&#x7684;&#x6570;&#x91CF;&#x3002;&#x4E3A;&#x4E86;&#x5229;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x6700;&#x5927;&#x5316;&#x53EF;&#x6269;&#x5C55;&#x6027;&#xFF0C;&#x9700;&#x8981;&#x5C06;&#x5DE5;&#x4F5C;&#x5757;&#x5212;&#x5206;&#x4E3A;&#x6700;&#x5C0F;&#x5DE5;&#x4F5C;&#x5757;&#x3002;&#x5F53;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7EBF;&#x7A0B;&#x4E0D;&#x591A;&#x65F6;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x5C06;&#x4F1A;&#x5904;&#x7406;&#x591A;&#x4E2A;&#x5DE5;&#x4F5C;&#x5757;&#xFF0C;&#x4E0D;&#x8FC7;&#x968F;&#x7740;&#x786C;&#x4EF6;&#x53EF;&#x7528;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;&#x7684;&#x589E;&#x957F;&#xFF0C;&#x4F1A;&#x6709;&#x8D8A;&#x6765;&#x8D8A;&#x591A;&#x7684;&#x5DE5;&#x4F5C;&#x5757;&#x5E76;&#x53D1;&#x6267;&#x884C;&#x3002;</p>
<p>&#x5F53;&#x6709;&#x201C;&#x6D4B;&#x8BD5;&#x5E76;&#x53D1;&#x6267;&#x884C;&#x6700;&#x5C0F;&#x5DE5;&#x4F5C;&#x5757;&#x201D;&#x7684;&#x60F3;&#x6CD5;&#x65F6;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x8C28;&#x614E;&#x4E86;&#x3002;&#x5411;&#x7EBF;&#x7A0B;&#x6C60;&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;&#x6709;&#x4E00;&#x5B9A;&#x7684;&#x5F00;&#x9500;&#xFF0C;&#x8BA9;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x8FD4;&#x56DE;&#x503C;&#x4FDD;&#x5B58;&#x5728;<code>std::future&lt;&gt;</code>&#x4E2D;&#xFF0C;&#x5BF9;&#x4E8E;&#x592A;&#x5C0F;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x5F00;&#x9500;&#x4E0D;&#x5212;&#x7B97;&#x3002;&#x5982;&#x679C;&#x4EFB;&#x52A1;&#x5757;&#x592A;&#x5C0F;&#xFF0C;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x901F;&#x5EA6;&#x53EF;&#x80FD;&#x90FD;&#x4E0D;&#x53CA;&#x5355;&#x7EBF;&#x7A0B;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4EFB;&#x52A1;&#x5757;&#x7684;&#x5927;&#x5C0F;&#x5408;&#x7406;&#xFF0C;&#x5C31;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#x4E86;&#xFF1A;&#x6253;&#x5305;&#x4EFB;&#x52A1;&#x3001;&#x83B7;&#x53D6;future&#x6216;&#x5B58;&#x50A8;&#x4E4B;&#x540E;&#x8981;&#x6C47;&#x5165;&#x7684;<code>std::thread</code>&#x5BF9;&#x8C61;&#x3002;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x4E9B;&#x90FD;&#x8981;&#x6CE8;&#x610F;&#x3002;&#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x662F;&#x8C03;&#x7528;submit()&#x6765;&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;&#x2461;&#x3002;</p>
<p>&#x7EBF;&#x7A0B;&#x6C60;&#x4E5F;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x5F02;&#x5E38;&#x5B89;&#x5168;&#x3002;&#x4EFB;&#x4F55;&#x5F02;&#x5E38;&#x90FD;&#x4F1A;&#x901A;&#x8FC7;submit()&#x8FD4;&#x56DE;&#x7ED9;future&#xFF0C;&#x5E76;&#x5728;&#x83B7;&#x53D6;future&#x7ED3;&#x679C;&#x65F6;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x3002;&#x5982;&#x679C;&#x51FD;&#x6570;&#x56E0;&#x4E3A;&#x5F02;&#x5E38;&#x9000;&#x51FA;&#xFF0C;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4F1A;&#x4E22;&#x6389;&#x90A3;&#x4E9B;&#x6CA1;&#x6709;&#x5B8C;&#x6210;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x5DE5;&#x4F5C;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;&#x6C60;&#x8FD8;&#x4E0D;&#x9519;&#xFF0C;&#x56E0;&#x4E3A;&#x4EFB;&#x52A1;&#x90FD;&#x662F;&#x76F8;&#x4E92;&#x72EC;&#x7ACB;&#x7684;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;&#x5F53;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#x6709;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x9047;&#x5230;&#x9EBB;&#x70E6;&#x4E86;&#x3002;</p>
<h2 id="913-&#x7B49;&#x5F85;&#x4F9D;&#x8D56;&#x4EFB;&#x52A1;">9.1.3 &#x7B49;&#x5F85;&#x4F9D;&#x8D56;&#x4EFB;&#x52A1;</h2>
<p>&#x4EE5;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x4E3A;&#x4F8B;&#xFF1A;&#x6570;&#x636E;&#x4E0E;&#x4E2D;&#x8F74;&#x6570;&#x636E;&#x9879;&#x6BD4;&#x8F83;&#xFF0C;&#x5728;&#x4E2D;&#x8F74;&#x9879;&#x4E24;&#x4FA7;&#x5206;&#x4E3A;&#x5927;&#x4E8E;&#x548C;&#x5C0F;&#x4E8E;&#x7684;&#x4E24;&#x4E2A;&#x5E8F;&#x5217;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5BF9;&#x8FD9;&#x4E24;&#x7EC4;&#x5E8F;&#x5217;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x8FD9;&#x4E24;&#x7EC4;&#x5E8F;&#x5217;&#x4F1A;&#x9012;&#x5F52;&#x6392;&#x5E8F;&#xFF0C;&#x6700;&#x540E;&#x4F1A;&#x6574;&#x5408;&#x6210;&#x4E00;&#x4E2A;&#x5168;&#x6392;&#x5E8F;&#x5E8F;&#x5217;&#x3002;&#x8981;&#x5C06;&#x8FD9;&#x4E2A;&#x7B97;&#x6CD5;&#x5199;&#x6210;&#x5E76;&#x53D1;&#x6A21;&#x5F0F;&#xFF0C;&#x9700;&#x8981;&#x4FDD;&#x8BC1;&#x9012;&#x5F52;&#x8C03;&#x7528;&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x786C;&#x4EF6;&#x7684;&#x5E76;&#x53D1;&#x80FD;&#x529B;&#x3002;</p>
<p>&#x56DE;&#x5230;&#x7B2C;4&#x7AE0;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x63A5;&#x89E6;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x4F7F;&#x7528;<code>std::async</code>&#x6765;&#x6267;&#x884C;&#x6BCF;&#x4E00;&#x5C42;&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x8BA9;&#x6807;&#x51C6;&#x5E93;&#x6765;&#x9009;&#x62E9;&#xFF0C;&#x662F;&#x5728;&#x65B0;&#x7EBF;&#x7A0B;&#x4E0A;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x8FD8;&#x662F;&#x5F53;&#x5BF9;&#x5E94;get()&#x8C03;&#x7528;&#x65F6;&#x8FDB;&#x884C;&#x540C;&#x6B65;&#x6267;&#x884C;&#x3002;&#x8FD0;&#x884C;&#x8D77;&#x6765;&#x5F88;&#x4E0D;&#x9519;&#xFF0C;&#x56E0;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x90FD;&#x5728;&#x5176;&#x81EA;&#x5DF1;&#x7684;&#x7EBF;&#x7A0B;&#x4E0A;&#x6267;&#x884C;&#xFF0C;&#x6216;&#x9700;&#x8981;&#x7684;&#x65F6;&#x5019;&#x8FDB;&#x884C;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x56DE;&#x987E;&#x7B2C;8&#x7AE0;&#xFF0C;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x56FA;&#x5B9A;&#x7EBF;&#x7A0B;&#x6570;&#x91CF;(&#x6839;&#x636E;&#x786C;&#x4EF6;&#x53EF;&#x7528;&#x5E76;&#x53D1;&#x7EBF;&#x7A0B;&#x6570;)&#x7684;&#x7ED3;&#x6784;&#x4F53;&#x3002;&#x8FD9;&#x6837;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F7F;&#x7528;&#x6808;&#x6765;&#x6302;&#x8D77;&#x9700;&#x8981;&#x6392;&#x5E8F;&#x7684;&#x6570;&#x636E;&#x5757;&#x3002;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x6570;&#x636E;&#x5757;&#x6392;&#x5E8F;&#x524D;&#xFF0C;&#x4F1A;&#x5411;&#x6570;&#x636E;&#x6808;&#x4E0A;&#x6DFB;&#x52A0;&#x4E00;&#x7EC4;&#x8981;&#x6392;&#x5E8F;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x5BF9;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5757;&#x6392;&#x5E8F;&#x7ED3;&#x675F;&#x540E;&#xFF0C;&#x63A5;&#x7740;&#x5BF9;&#x53E6;&#x4E00;&#x5757;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x8FD9;&#x4F1A;&#x6D88;&#x8017;&#x6709;&#x9650;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x6240;&#x4EE5;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x6392;&#x5E8F;&#x53EF;&#x80FD;&#x4F1A;&#x9020;&#x6210;&#x6B7B;&#x9501;&#x3002;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#x5F88;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#xFF0C;&#x5C31;&#x662F;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD;&#x5728;&#x7B49;&#x67D0;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5757;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x4E0D;&#x8FC7;&#x6CA1;&#x6709;&#x7EBF;&#x7A0B;&#x5728;&#x505A;&#x8FD9;&#x5757;&#x6570;&#x636E;&#x7684;&#x6392;&#x5E8F;&#x3002;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x62C9;&#x53D6;&#x6808;&#x4E0A;&#x6570;&#x636E;&#x5757;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5BF9;&#x6570;&#x636E;&#x5757;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x5982;&#x679C;&#x53EA;&#x7528;&#x7B80;&#x5355;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x8FDB;&#x884C;&#x66FF;&#x6362;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;&#x7B2C;4&#x7AE0;&#x66FF;&#x6362;<code>std::async</code>&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x3002;&#x53EA;&#x6709;&#x56FA;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x56E0;&#x4E3A;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x6CA1;&#x6709;&#x7A7A;&#x95F2;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x7EBF;&#x7A0B;&#x4F1A;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x9700;&#x8981;&#x6709;&#x548C;&#x7B2C;8&#x7AE0;&#x4E2D;&#x7C7B;&#x4F3C;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF1A;&#x5F53;&#x7B49;&#x5F85;&#x67D0;&#x4E2A;&#x6570;&#x636E;&#x5757;&#x5B8C;&#x6210;&#x65F6;&#xFF0C;&#x53BB;&#x5904;&#x7406;&#x672A;&#x5B8C;&#x6210;&#x7684;&#x6570;&#x636E;&#x5757;&#x3002;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x6765;&#x7BA1;&#x7406;&#x4EFB;&#x52A1;&#x5217;&#x8868;&#x548C;&#x76F8;&#x5173;&#x7EBF;&#x7A0B;&#xFF0C;&#x5C31;&#x4E0D;&#x7528;&#x518D;&#x53BB;&#x8BBF;&#x95EE;&#x4EFB;&#x52A1;&#x5217;&#x8868;&#x4E86;&#x3002;&#x53EF;&#x4EE5;&#x5BF9;&#x7EBF;&#x7A0B;&#x6C60;&#x505A;&#x4E00;&#x4E9B;&#x6539;&#x52A8;&#xFF0C;&#x81EA;&#x52A8;&#x5B8C;&#x6210;&#x8FD9;&#x4E9B;&#x4E8B;&#x60C5;&#x3002;</p>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x5728;thread_pool&#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B0;&#x51FD;&#x6570;&#xFF0C;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x5E76;&#x5BF9;&#x7EBF;&#x7A0B;&#x6C60;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;&#x9AD8;&#x7EA7;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x5B9E;&#x73B0;&#x53EF;&#x80FD;&#x4F1A;&#x5728;&#x7B49;&#x5F85;&#x51FD;&#x6570;&#x4E2D;&#x6DFB;&#x52A0;&#x903B;&#x8F91;&#xFF0C;&#x6216;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x6765;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x4F18;&#x5148;&#x7684;&#x4EFB;&#x52A1;&#x4F1A;&#x8BA9;&#x5176;&#x4ED6;&#x7684;&#x4EFB;&#x52A1;&#x8FDB;&#x884C;&#x7B49;&#x5F85;&#x3002;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x5C55;&#x793A;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;run_pending_task()&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x7684;&#x4FEE;&#x6539;&#x5C06;&#x4F1A;&#x5728;&#x4EE3;&#x7801;9.5&#x4E2D;&#x5C55;&#x793A;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.4 run_pending_task()&#x51FD;&#x6570;&#x5B9E;&#x73B0;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">void</span> thread_pool::run_pending_task()
{
  function_wrapper task;
  <span class="hljs-keyword">if</span>(work_queue.try_pop(task))
  {
    task();
  }
  <span class="hljs-keyword">else</span>
  {
    <span class="hljs-built_in">std</span>::this_thread::yield();
  }
}
</code></pre>
<p>run_pending_task()&#x7684;&#x5B9E;&#x73B0;&#x53BB;&#x6389;&#x4E86;&#x5728;worker_thread()&#x51FD;&#x6570;&#x7684;&#x4E3B;&#x5FAA;&#x73AF;&#x3002;&#x51FD;&#x6570;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x6709;&#x4EFB;&#x52A1;&#x7684;&#x65F6;&#x5019;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#xFF0C;&#x6CA1;&#x6709;&#x7684;&#x8BDD;&#x5C31;&#x4F1A;&#x8BA9;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x5BF9;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x91CD;&#x65B0;&#x5206;&#x914D;&#x3002;&#x4E0B;&#x9762;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x8981;&#x6BD4;&#x4EE3;&#x7801;8.1&#x4E2D;&#x7248;&#x672C;&#x7B80;&#x5355;&#x8BB8;&#x591A;&#xFF0C;&#x56E0;&#x4E3A;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;&#x903B;&#x8F91;&#x90FD;&#x79FB;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x4E86;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.5 &#x57FA;&#x4E8E;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x5B9E;&#x73B0;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
<span class="hljs-keyword">struct</span> sorter  <span class="hljs-comment">// 1</span>
{
  thread_pool pool;  <span class="hljs-comment">// 2</span>

  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; do_sort(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt;&amp; chunk_data)
  {
    <span class="hljs-keyword">if</span>(chunk_data.empty())
    {
      <span class="hljs-keyword">return</span> chunk_data;
    }

    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; result;
    result.splice(result.begin(),chunk_data,chunk_data.begin());
    T <span class="hljs-keyword">const</span>&amp; partition_val=*result.begin();

    <span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt;::iterator divide_point=
      <span class="hljs-built_in">std</span>::partition(chunk_data.begin(),chunk_data.end(),
                     [&amp;](T <span class="hljs-keyword">const</span>&amp; val){<span class="hljs-keyword">return</span> val&lt;partition_val;});

    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; new_lower_chunk;
    new_lower_chunk.splice(new_lower_chunk.end(),
                           chunk_data,chunk_data.begin(),
                           divide_point);

    <span class="hljs-built_in">std</span>::future&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; &gt; new_lower=  <span class="hljs-comment">// 3</span>
      pool.submit(<span class="hljs-built_in">std</span>::bind(&amp;sorter::do_sort,<span class="hljs-keyword">this</span>,
                            <span class="hljs-built_in">std</span>::move(new_lower_chunk)));

    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; new_higher(do_sort(chunk_data));

    result.splice(result.end(),new_higher);
    <span class="hljs-keyword">while</span>(!new_lower.wait_for(<span class="hljs-built_in">std</span>::chrono::seconds(<span class="hljs-number">0</span>)) ==
      <span class="hljs-built_in">std</span>::future_status::timeout)
    {
      pool.run_pending_task();  <span class="hljs-comment">// 4</span>
    }

    result.splice(result.begin(),new_lower.get());
    <span class="hljs-keyword">return</span> result;
  }
};

<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
<span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; parallel_quick_sort(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">list</span>&lt;T&gt; input)
{
  <span class="hljs-keyword">if</span>(input.empty())
  {
    <span class="hljs-keyword">return</span> input;
  }
  sorter&lt;T&gt; s;

  <span class="hljs-keyword">return</span> s.do_sort(input);
}
</code></pre>
<p>&#x4E0E;&#x4EE3;&#x7801;8.1&#x76F8;&#x6BD4;&#xFF0C;&#x8FD9;&#x91CC;&#x5C06;&#x5B9E;&#x9645;&#x5DE5;&#x4F5C;&#x653E;&#x5728;sorter&#x7C7B;&#x6A21;&#x677F;&#x7684;do_sort()&#x6210;&#x5458;&#x51FD;&#x6570;&#x4E2D;&#x6267;&#x884C;&#x2460;&#xFF0C;&#x5373;&#x4F7F;&#x4F8B;&#x5B50;&#x4E2D;&#x4EC5;&#x5BF9;thread_pool&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x5305;&#x88C5;&#x2461;&#x3002;</p>
<p>&#x7EBF;&#x7A0B;&#x548C;&#x4EFB;&#x52A1;&#x7BA1;&#x7406;&#x5728;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x4F1A;&#x5C11;&#x5411;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x2462;&#xFF0C;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x672A;&#x5B8C;&#x6210;&#x7684;&#x4EFB;&#x52A1;&#x2463;&#xFF0C;&#x9700;&#x8981;&#x663E;&#x5F0F;&#x7684;&#x7BA1;&#x7406;&#x7EBF;&#x7A0B;&#x548C;&#x6808;&#x4E0A;&#x8981;&#x6392;&#x5E8F;&#x7684;&#x6570;&#x636E;&#x5757;&#x3002;&#x5F53;&#x6709;&#x4EFB;&#x52A1;&#x63D0;&#x4EA4;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>std::bind()</code>&#x7ED1;&#x5B9A;this&#x6307;&#x9488;&#x5230;do_sort()&#x4E0A;&#xFF0C;&#x7ED1;&#x5B9A;&#x662F;&#x4E3A;&#x4E86;&#x8BA9;&#x6570;&#x636E;&#x5757;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x9700;&#x8981;&#x5BF9;new_lower_chunk&#x4F7F;&#x7528;<code>std::move()</code>&#x5C06;&#x5176;&#x4F20;&#x5165;&#x51FD;&#x6570;&#xFF0C;&#x6570;&#x636E;&#x79FB;&#x52A8;&#x8981;&#x6BD4;&#x62F7;&#x8D1D;&#x7684;&#x5F00;&#x9500;&#x5C11;&#x3002;</p>
<p>&#x867D;&#x7136;&#xFF0C;&#x4F7F;&#x7528;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x7684;&#x65B9;&#x5F0F;&#x89E3;&#x51B3;&#x4E86;&#x6B7B;&#x9501;&#xFF0C;&#x4F46;&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;&#x6C60;&#x8DDD;&#x79BB;&#x7406;&#x60F3;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x8FD8;&#x5DEE;&#x5F88;&#x8FDC;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6BCF;&#x6B21;&#x5BF9;submit()&#x7684;&#x8C03;&#x7528;&#x548C;&#x5BF9;run_pending_task()&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x8BBF;&#x95EE;&#x7684;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x3002;&#x7B2C;8&#x7AE0;&#x4E2D;&#xFF0C;&#x5F53;&#x591A;&#x7EBF;&#x7A0B;&#x53BB;&#x4FEE;&#x6539;&#x4E00;&#x7EC4;&#x6570;&#x636E;&#xFF0C;&#x5C31;&#x4F1A;&#x5BF9;&#x6027;&#x80FD;&#x6709;&#x6240;&#x5F71;&#x54CD;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x4E5F;&#x662F;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<h2 id="914-&#x907F;&#x514D;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#x7ADE;&#x4E89;">9.1.4 &#x907F;&#x514D;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#x7ADE;&#x4E89;</h2>
<p>&#x7EBF;&#x7A0B;&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;submit()&#xFF0C;&#x90FD;&#x4F1A;&#x63A8;&#x9001;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x5230;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x4E2D;&#x3002;&#x5C31;&#x50CF;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x4E3A;&#x4E86;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#xFF0C;&#x4ECE;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#x4E00;&#x6837;&#x3002;&#x968F;&#x7740;&#x5904;&#x7406;&#x5668;&#x7684;&#x589E;&#x52A0;&#xFF0C;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#x5C31;&#x4F1A;&#x6709;&#x5F88;&#x591A;&#x7684;&#x7ADE;&#x4E89;&#xFF0C;&#x4F1A;&#x8BA9;&#x6027;&#x80FD;&#x4E0B;&#x964D;&#x3002;&#x4F7F;&#x7528;&#x65E0;&#x9501;&#x961F;&#x5217;&#x4F1A;&#x8BA9;&#x4EFB;&#x52A1;&#x6CA1;&#x6709;&#x660E;&#x663E;&#x7684;&#x7B49;&#x5F85;&#xFF0C;&#x4F46;&#x4E52;&#x4E53;&#x7F13;&#x5B58;&#x4F1A;&#x6D88;&#x8017;&#x5927;&#x91CF;&#x7684;&#x65F6;&#x95F4;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x4E52;&#x4E53;&#x7F13;&#x5B58;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x5EFA;&#x7ACB;&#x72EC;&#x7ACB;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x5C31;&#x4F1A;&#x5C06;&#x65B0;&#x4EFB;&#x52A1;&#x653E;&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E0A;&#xFF0C;&#x5E76;&#x4E14;&#x5F53;&#x7EBF;&#x7A0B;&#x4E0A;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x6CA1;&#x6709;&#x4EFB;&#x52A1;&#x65F6;&#xFF0C;&#x53BB;&#x5168;&#x5C40;&#x7684;&#x4EFB;&#x52A1;&#x5217;&#x8868;&#x4E2D;&#x53D6;&#x4EFB;&#x52A1;&#x3002;&#x4E0B;&#x9762;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x4F7F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;thread_local&#x53D8;&#x91CF;&#xFF0C;&#x6765;&#x4FDD;&#x8BC1;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x62E5;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x4EFB;&#x52A1;&#x5217;&#x8868;(&#x5982;&#x5168;&#x5C40;&#x5217;&#x8868;&#x90A3;&#x6837;)&#x3002;</p>
<p>&#x4EE3;&#x7801;9.6 &#x7EBF;&#x7A0B;&#x6C60;&#x2014;&#x2014;&#x7EBF;&#x7A0B;&#x5177;&#x6709;&#x672C;&#x5730;&#x4EFB;&#x52A1;&#x961F;&#x5217;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> thread_pool
{
  thread_safe_queue&lt;function_wrapper&gt; pool_work_queue;

  <span class="hljs-keyword">typedef</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">queue</span>&lt;function_wrapper&gt; local_queue_type;  <span class="hljs-comment">// 1</span>
  <span class="hljs-keyword">static</span> <span class="hljs-keyword">thread_local</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">unique_ptr</span>&lt;local_queue_type&gt;
    local_work_queue;  <span class="hljs-comment">// 2</span>

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">worker_thread</span><span class="hljs-params">()</span>
  </span>{
    local_work_queue.reset(<span class="hljs-keyword">new</span> local_queue_type);  <span class="hljs-comment">// 3</span>
    <span class="hljs-keyword">while</span>(!done)
    {
      run_pending_task();
    }
  }

<span class="hljs-keyword">public</span>:
  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> FunctionType&gt;
  <span class="hljs-built_in">std</span>::future&lt;<span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::result_of&lt;FunctionType()&gt;::type&gt;
    submit(FunctionType f)
  {
    <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::result_of&lt;FunctionType()&gt;::type result_type;

    <span class="hljs-built_in">std</span>::packaged_task&lt;result_type()&gt; task(f);
    <span class="hljs-built_in">std</span>::future&lt;result_type&gt; res(task.get_future());
    <span class="hljs-keyword">if</span>(local_work_queue)  <span class="hljs-comment">// 4</span>
    {
      local_work_queue-&gt;push(<span class="hljs-built_in">std</span>::move(task));
    }
    <span class="hljs-keyword">else</span>
    {
      pool_work_queue.push(<span class="hljs-built_in">std</span>::move(task));  <span class="hljs-comment">// 5</span>
    }
    <span class="hljs-keyword">return</span> res;
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">run_pending_task</span><span class="hljs-params">()</span>
  </span>{
    function_wrapper task;
    <span class="hljs-keyword">if</span>(local_work_queue &amp;&amp; !local_work_queue-&gt;empty())  <span class="hljs-comment">// 6</span>
    {
      task=<span class="hljs-built_in">std</span>::move(local_work_queue-&gt;front());
      local_work_queue-&gt;pop();
      task();
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(pool_work_queue.try_pop(task))  <span class="hljs-comment">// 7</span>
    {
      task();
    }
    <span class="hljs-keyword">else</span>
    {
      <span class="hljs-built_in">std</span>::this_thread::yield();
    }
  }
<span class="hljs-comment">// rest as before</span>
};
</code></pre>
<p>&#x56E0;&#x4E3A;&#x4E0D;&#x5E0C;&#x671B;&#x975E;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x7EBF;&#x7A0B;&#x4E5F;&#x62E5;&#x6709;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x4F7F;&#x7528;<code>std::unique_ptr&lt;&gt;</code>&#x6307;&#x5411;&#x7EBF;&#x7A0B;&#x672C;&#x5730;&#x7684;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x2461;&#xFF0C;&#x8FD9;&#x4E2A;&#x6307;&#x9488;&#x5728;worker_thread()&#x4E2D;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#x2462;&#x3002;<code>std:unique_ptr&lt;&gt;</code>&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4F1A;&#x4FDD;&#x8BC1;&#x5728;&#x7EBF;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#x9500;&#x6BC1;&#x961F;&#x5217;&#x3002;</p>
<p>submit()&#x4F1A;&#x68C0;&#x67E5;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x5177;&#x6709;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x2463;&#x3002;&#x5982;&#x679C;&#x6709;&#xFF0C;&#x5C31;&#x662F;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x4EFB;&#x52A1;&#x653E;&#x5165;&#x7EBF;&#x7A0B;&#x7684;&#x672C;&#x5730;&#x961F;&#x5217;&#x4E2D;&#xFF1B;&#x5426;&#x5219;&#xFF0C;&#x5C06;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#x653E;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x5168;&#x5C40;&#x961F;&#x5217;&#x4E2D;&#x2464;&#x3002;</p>
<p>run_pending_task()&#x2465;&#x4E2D;&#x7684;&#x68C0;&#x67E5;&#x548C;&#x4E4B;&#x524D;&#x7C7B;&#x4F3C;&#xFF0C;&#x53EF;&#x4EE5;&#x5BF9;&#x662F;&#x5426;&#x5B58;&#x5728;&#x672C;&#x5730;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#x3002;&#x5982;&#x679C;&#x5B58;&#x5728;&#xFF0C;&#x5C31;&#x4F1A;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x3002;&#x6CE8;&#x610F;&#x672C;&#x5730;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x7684;<code>std::queue&lt;&gt;</code>&#x2460;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x53EA;&#x80FD;&#x88AB;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6240;&#x8BBF;&#x95EE;&#xFF0C;&#x5C31;&#x4E0D;&#x5B58;&#x5728;&#x7ADE;&#x4E89;&#x3002;&#x5982;&#x679C;&#x672C;&#x5730;&#x7EBF;&#x7A0B;&#x4E0A;&#x6CA1;&#x6709;&#x4EFB;&#x52A1;&#xFF0C;&#x5C31;&#x4F1A;&#x4ECE;&#x5168;&#x5C40;&#x5DE5;&#x4F5C;&#x5217;&#x8868;&#x4E0A;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#x2466;&#x3002;</p>
<p>&#x8FD9;&#x6837;&#x5C31;&#x80FD;&#x6709;&#x6548;&#x7684;&#x907F;&#x514D;&#x7ADE;&#x4E89;&#xFF0C;&#x4E0D;&#x8FC7;&#x5F53;&#x4EFB;&#x52A1;&#x5206;&#x914D;&#x4E0D;&#x5747;&#x65F6;&#xFF0C;&#x9020;&#x6210;&#x7684;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#xFF1A;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x672C;&#x5730;&#x961F;&#x5217;&#x4E2D;&#x6709;&#x5F88;&#x591A;&#x4EFB;&#x52A1;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x65E0;&#x6240;&#x4E8B;&#x4E8B;&#x3002;&#x4F8B;&#x5982;&#xFF1A;&#x4E3E;&#x4E00;&#x4E2A;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x4E00;&#x5F00;&#x59CB;&#x7684;&#x6570;&#x636E;&#x5757;&#x80FD;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x4E0A;&#x88AB;&#x5904;&#x7406;&#xFF0C;&#x56E0;&#x4E3A;&#x5269;&#x4F59;&#x90E8;&#x5206;&#x4F1A;&#x653E;&#x5728;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x7684;&#x672C;&#x5730;&#x961F;&#x5217;&#x4E0A;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x4E5F;&#x8FDD;&#x80CC;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x521D;&#x8877;&#x3002;</p>
<p>&#x5E78;&#x597D;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x6709;&#x89E3;&#xFF1A;&#x672C;&#x5730;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x548C;&#x5168;&#x5C40;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x4E0A;&#x6CA1;&#x6709;&#x4EFB;&#x52A1;&#x65F6;&#xFF0C;&#x53EF;&#x4ECE;&#x522B;&#x7684;&#x7EBF;&#x7A0B;&#x961F;&#x5217;&#x4E2D;&#x7A83;&#x53D6;&#x4EFB;&#x52A1;&#x3002;</p>
<h2 id="915-&#x7A83;&#x53D6;&#x4EFB;&#x52A1;">9.1.5 &#x7A83;&#x53D6;&#x4EFB;&#x52A1;</h2>
<p>&#x4E3A;&#x4E86;&#x8BA9;&#x6CA1;&#x6709;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x4ECE;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x672C;&#x5730;&#x4EFB;&#x52A1;&#x5217;&#x8868;&#x53EF;&#x4EE5;&#x88AB;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#xFF0C;&#x8FD9;&#x6837;&#x624D;&#x80FD;&#x8BA9;run_pending_tasks()&#x7A83;&#x53D6;&#x4EFB;&#x52A1;&#x3002;&#x9700;&#x8981;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x961F;&#x5217;&#x4E0A;&#x8FDB;&#x884C;&#x6CE8;&#x518C;&#xFF0C;&#x6216;&#x7531;&#x7EBF;&#x7A0B;&#x6C60;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#x9002;&#x5F53;&#x7684;&#x88AB;&#x540C;&#x6B65;&#x548C;&#x4FDD;&#x62A4;&#xFF0C;&#x8FD9;&#x6837;&#x961F;&#x5217;&#x7684;&#x4E0D;&#x53D8;&#x91CF;&#x5C31;&#x4E0D;&#x4F1A;&#x88AB;&#x7834;&#x574F;&#x3002;</p>
<p>&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x65E0;&#x9501;&#x961F;&#x5217;&#xFF0C;&#x8BA9;&#x5176;&#x7EBF;&#x7A0B;&#x5728;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4E0A;&#x7A83;&#x53D6;&#x4EFB;&#x52A1;&#x65F6;&#xFF0C;&#x6709;&#x63A8;&#x9001;&#x548C;&#x5F39;&#x51FA;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x7684;&#x53EF;&#x80FD;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x7684;&#x5B9E;&#x73B0;&#x8D85;&#x51FA;&#x4E86;&#x672C;&#x4E66;&#x7684;&#x8BA8;&#x8BBA;&#x8303;&#x56F4;&#x3002;&#x4E3A;&#x4E86;&#x8BC1;&#x660E;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x7684;&#x53EF;&#x884C;&#x6027;&#xFF0C;&#x5C06;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x4E92;&#x65A5;&#x91CF;&#x6765;&#x4FDD;&#x62A4;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x3002;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x4EFB;&#x52A1;&#x7A83;&#x53D6;&#x662F;&#x4E0D;&#x5E38;&#x89C1;&#x7684;&#x73B0;&#x8C61;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x4F1A;&#x51CF;&#x5C11;&#x5BF9;&#x4E92;&#x65A5;&#x91CF;&#x7684;&#x7ADE;&#x4E89;&#xFF0C;&#x5E76;&#x4E14;&#x4F7F;&#x5F97;&#x7B80;&#x5355;&#x961F;&#x5217;&#x7684;&#x5F00;&#x9500;&#x6700;&#x5C0F;&#x3002;&#x4E0B;&#x9762;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x57FA;&#x4E8E;&#x9501;&#x7684;&#x4EFB;&#x52A1;&#x7A83;&#x53D6;&#x961F;&#x5217;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.7 &#x57FA;&#x4E8E;&#x9501;&#x7684;&#x4EFB;&#x52A1;&#x7A83;&#x53D6;&#x961F;&#x5217;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> work_stealing_queue
{
<span class="hljs-keyword">private</span>:
  <span class="hljs-keyword">typedef</span> function_wrapper data_type;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">deque</span>&lt;data_type&gt; the_queue;  <span class="hljs-comment">// 1</span>
  <span class="hljs-keyword">mutable</span> <span class="hljs-built_in">std</span>::mutex the_mutex;

<span class="hljs-keyword">public</span>:
  work_stealing_queue()
  {}

  work_stealing_queue(<span class="hljs-keyword">const</span> work_stealing_queue&amp; other)=<span class="hljs-keyword">delete</span>;
  work_stealing_queue&amp; <span class="hljs-keyword">operator</span>=(
    <span class="hljs-keyword">const</span> work_stealing_queue&amp; other)=<span class="hljs-keyword">delete</span>;

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">push</span><span class="hljs-params">(data_type data)</span>  <span class="hljs-comment">// 2</span>
  </span>{
    <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lock(the_mutex);
    the_queue.push_front(<span class="hljs-built_in">std</span>::move(data));
  }

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">empty</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span>
  </span>{
    <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lock(the_mutex);
    <span class="hljs-keyword">return</span> the_queue.empty();
  }

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">try_pop</span><span class="hljs-params">(data_type&amp; res)</span>  <span class="hljs-comment">// 3</span>
  </span>{
    <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lock(the_mutex);
    <span class="hljs-keyword">if</span>(the_queue.empty())
    {
      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }

    res=<span class="hljs-built_in">std</span>::move(the_queue.front());
    the_queue.pop_front();
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  }

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">try_steal</span><span class="hljs-params">(data_type&amp; res)</span>  <span class="hljs-comment">// 4</span>
  </span>{
    <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lock(the_mutex);
    <span class="hljs-keyword">if</span>(the_queue.empty())
    {
      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }

    res=<span class="hljs-built_in">std</span>::move(the_queue.back());
    the_queue.pop_back();
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  }
};
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x5BF9;<code>std::deque&lt;fuction_wrapper&gt;</code>&#x8FDB;&#x884C;&#x4E86;&#x7B80;&#x5355;&#x7684;&#x5305;&#x88C5;&#x2460;&#xFF0C;&#x80FD;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x4E92;&#x65A5;&#x9501;&#x6765;&#x5BF9;&#x6240;&#x6709;&#x8BBF;&#x95EE;&#x8FDB;&#x884C;&#x63A7;&#x5236;&#x4E86;&#x3002;push()&#x2461;&#x548C;try_pop()&#x2462;&#x5BF9;&#x961F;&#x5217;&#x7684;&#x524D;&#x7AEF;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;try_steal()&#x2463;&#x5BF9;&#x961F;&#x5217;&#x7684;&#x540E;&#x7AEF;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x8FD9;&#x5C31;&#x8BF4;&#x660E;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x7684;&#x201C;&#x961F;&#x5217;&#x201D;&#x662F;&#x4E00;&#x4E2A;&#x540E;&#x8FDB;&#x5148;&#x51FA;&#x7684;&#x6808;&#xFF0C;&#x6700;&#x65B0;&#x63A8;&#x5165;&#x7684;&#x4EFB;&#x52A1;&#x5C06;&#x4F1A;&#x7B2C;&#x4E00;&#x4E2A;&#x6267;&#x884C;&#x3002;&#x4ECE;&#x7F13;&#x5B58;&#x89D2;&#x5EA6;&#x6765;&#x770B;&#xFF0C;&#x8FD9;&#x5C06;&#x5BF9;&#x6027;&#x80FD;&#x6709;&#x6240;&#x63D0;&#x5347;&#xFF0C;&#x56E0;&#x4E3A;&#x4EFB;&#x52A1;&#x76F8;&#x5173;&#x7684;&#x6570;&#x636E;&#x4E00;&#x76F4;&#x5B58;&#x4E8E;&#x7F13;&#x5B58;&#x4E2D;&#xFF0C;&#x8981;&#x6BD4;&#x63D0;&#x524D;&#x5C06;&#x4EFB;&#x52A1;&#x76F8;&#x5173;&#x6570;&#x636E;&#x63A8;&#x9001;&#x5230;&#x6808;&#x4E0A;&#x597D;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x5F88;&#x597D;&#x7684;&#x6620;&#x5C04;&#x5230;&#x67D0;&#x4E2A;&#x7B97;&#x6CD5;&#x4E0A;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;&#x5FEB;&#x901F;&#x6392;&#x5E8F;&#x3002;&#x4E4B;&#x524D;&#x7684;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x6BCF;&#x6B21;&#x8C03;&#x7528;do_sort()&#x90FD;&#x4F1A;&#x63A8;&#x9001;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x5230;&#x6808;&#x4E0A;&#xFF0C;&#x5E76;&#x4E14;&#x7B49;&#x5F85;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x3002;&#x901A;&#x8FC7;&#x5BF9;&#x6700;&#x65B0;&#x63A8;&#x5165;&#x4EFB;&#x52A1;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x5728;&#x5C06;&#x5F53;&#x524D;&#x6240;&#x9700;&#x6570;&#x636E;&#x5757;&#x5904;&#x7406;&#x5B8C;&#x6210;&#x524D;&#xFF0C;&#x5176;&#x4ED6;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x9700;&#x8981;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x5757;&#xFF0C;&#x4ECE;&#x800C;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x6D3B;&#x52A8;&#x4EFB;&#x52A1;&#x7684;&#x6570;&#x91CF;&#x548C;&#x6808;&#x7684;&#x4F7F;&#x7528;&#x6B21;&#x6570;&#x3002;try_steal()&#x4ECE;&#x961F;&#x5217;&#x672B;&#x5C3E;&#x83B7;&#x53D6;&#x4EFB;&#x52A1;&#xFF0C;&#x4E3A;&#x4E86;&#x51CF;&#x5C11;&#x4E0E;try_pop()&#x4E4B;&#x95F4;&#x7684;&#x7ADE;&#x4E89;&#x3002;&#x4F7F;&#x7528;&#x5728;&#x7B2C;6&#x3001;7&#x7AE0;&#x4E2D;&#x7684;&#x6240;&#x8BA8;&#x8BBA;&#x7684;&#x6280;&#x672F;&#x6765;&#x8BA9;try_pop()&#x548C;try_steal()&#x5E76;&#x53D1;&#x6267;&#x884C;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x62E5;&#x6709;&#x4E86;&#x4E00;&#x4E2A;&#x5F88;&#x4E0D;&#x9519;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x5E76;&#x4E14;&#x652F;&#x6301;&#x7A83;&#x53D6;&#x3002;&#x90A3;&#x5982;&#x4F55;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x5462;&#xFF1F;&#x8FD9;&#x91CC;&#x7B80;&#x5355;&#x7684;&#x5C55;&#x793A;&#x4E00;&#x4E0B;&#x3002;</p>
<p>&#x4EE3;&#x7801;9.8 &#x4F7F;&#x7528;&#x4EFB;&#x52A1;&#x7A83;&#x53D6;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> thread_pool
{
  <span class="hljs-keyword">typedef</span> function_wrapper task_type;

  <span class="hljs-built_in">std</span>::<span class="hljs-keyword">atomic_bool</span> done;
  thread_safe_queue&lt;task_type&gt; pool_work_queue;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">unique_ptr</span>&lt;work_stealing_queue&gt; &gt; queues;  <span class="hljs-comment">// 1</span>
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">std</span>::thread&gt; threads;
  join_threads joiner;

  <span class="hljs-keyword">static</span> <span class="hljs-keyword">thread_local</span> work_stealing_queue* local_work_queue;  <span class="hljs-comment">// 2</span>
  <span class="hljs-keyword">static</span> <span class="hljs-keyword">thread_local</span> <span class="hljs-keyword">unsigned</span> my_index;

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">worker_thread</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> my_index_)</span>
  </span>{
    my_index=my_index_;
    local_work_queue=queues[my_index].get();  <span class="hljs-comment">// 3</span>
    <span class="hljs-keyword">while</span>(!done)
    {
      run_pending_task();
    }
  }

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">pop_task_from_local_queue</span><span class="hljs-params">(task_type&amp; task)</span>
  </span>{
    <span class="hljs-keyword">return</span> local_work_queue &amp;&amp; local_work_queue-&gt;try_pop(task);
  }

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">pop_task_from_pool_queue</span><span class="hljs-params">(task_type&amp; task)</span>
  </span>{
    <span class="hljs-keyword">return</span> pool_work_queue.try_pop(task);
  }

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">pop_task_from_other_thread_queue</span><span class="hljs-params">(task_type&amp; task)</span>  <span class="hljs-comment">// 4</span>
  </span>{
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">unsigned</span> i=<span class="hljs-number">0</span>;i&lt;queues.size();++i)
    {
      <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">const</span> index=(my_index+i+<span class="hljs-number">1</span>)%queues.size();  <span class="hljs-comment">// 5</span>
      <span class="hljs-keyword">if</span>(queues[index]-&gt;try_steal(task))
      {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
      }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }

<span class="hljs-keyword">public</span>:
  thread_pool():
    done(<span class="hljs-literal">false</span>),joiner(threads)
  {
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">const</span> thread_count=<span class="hljs-built_in">std</span>::thread::hardware_concurrency();

    <span class="hljs-keyword">try</span>
    {
      <span class="hljs-keyword">for</span>(<span class="hljs-keyword">unsigned</span> i=<span class="hljs-number">0</span>;i&lt;thread_count;++i)
      {
        queues.push_back(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">unique_ptr</span>&lt;work_stealing_queue&gt;(  <span class="hljs-comment">// 6</span>
                         <span class="hljs-keyword">new</span> work_stealing_queue));
        threads.push_back(
          <span class="hljs-built_in">std</span>::thread(&amp;thread_pool::worker_thread,<span class="hljs-keyword">this</span>,i));
      }
    }
    <span class="hljs-keyword">catch</span>(...)
    {
      done=<span class="hljs-literal">true</span>;
      <span class="hljs-keyword">throw</span>;
    }
  }

  ~thread_pool()
  {
    done=<span class="hljs-literal">true</span>;
  }

  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> FunctionType&gt;
  <span class="hljs-built_in">std</span>::future&lt;<span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::result_of&lt;FunctionType()&gt;::type&gt; submit(
    FunctionType f)
  { 
    <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">typename</span> <span class="hljs-built_in">std</span>::result_of&lt;FunctionType()&gt;::type result_type;
    <span class="hljs-built_in">std</span>::packaged_task&lt;result_type()&gt; task(f);
    <span class="hljs-built_in">std</span>::future&lt;result_type&gt; res(task.get_future());
    <span class="hljs-keyword">if</span>(local_work_queue)
    {
      local_work_queue-&gt;push(<span class="hljs-built_in">std</span>::move(task));
    }
    <span class="hljs-keyword">else</span>
    {
      pool_work_queue.push(<span class="hljs-built_in">std</span>::move(task));
    }
    <span class="hljs-keyword">return</span> res;
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">run_pending_task</span><span class="hljs-params">()</span>
  </span>{
    task_type task;
    <span class="hljs-keyword">if</span>(pop_task_from_local_queue(task) ||  <span class="hljs-comment">// 7</span>
       pop_task_from_pool_queue(task) ||  <span class="hljs-comment">// 8</span>
       pop_task_from_other_thread_queue(task))  <span class="hljs-comment">// 9</span>
    {
      task();
    }
    <span class="hljs-keyword">else</span>
    {
      <span class="hljs-built_in">std</span>::this_thread::yield();
    }
  }
};
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4E0E;&#x4EE3;&#x7801;9.6&#x5F88;&#x76F8;&#x4F3C;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x5728;&#x4E8E;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x6709;&#x4E00;&#x4E2A;work_stealing_queue&#xFF0C;&#x800C;&#x975E;&#x53EA;&#x662F;&#x666E;&#x901A;&#x7684;<code>std::queue&lt;&gt;</code>&#x2461;&#x3002;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x6709;&#x4E00;&#x4E2A;&#x5C5E;&#x4E8E;&#x81EA;&#x5DF1;&#x7684;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x2465;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x81EA;&#x5DF1;&#x7684;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x5C06;&#x5B58;&#x50A8;&#x5728;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x5168;&#x5C40;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x4E2D;&#x2460;&#x3002;&#x5217;&#x8868;&#x4E2D;&#x961F;&#x5217;&#x7684;&#x5E8F;&#x53F7;&#xFF0C;&#x4F1A;&#x4F20;&#x9012;&#x7ED9;&#x7EBF;&#x7A0B;&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x4F7F;&#x7528;&#x5E8F;&#x53F7;&#x6765;&#x7D22;&#x5F15;&#x961F;&#x5217;&#x2462;&#x3002;&#x4E3A;&#x4E86;&#x80FD;&#x8BA9;&#x95F2;&#x7F6E;&#x7684;&#x7EBF;&#x7A0B;&#x7A83;&#x53D6;&#x4EFB;&#x52A1;&#xFF0C;&#x7EBF;&#x7A0B;&#x6C60;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x4EFB;&#x610F;&#x7EBF;&#x7A0B;&#x4E2D;&#x7684;&#x961F;&#x5217;&#x3002;run_pending_task()&#x5C06;&#x4F1A;&#x4ECE;&#x7EBF;&#x7A0B;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#x53D6;&#x51FA;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x6765;&#x6267;&#x884C;&#x2466;&#xFF0C;&#x6216;&#x4ECE;&#x7EBF;&#x7A0B;&#x6C60;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x2467;&#xFF0C;&#x4EA6;&#x6216;&#x4ECE;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x7684;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x2468;&#x3002;</p>
<p>pop_task_from_other_thread_queue()&#x2463;&#x4F1A;&#x904D;&#x5386;&#x6C60;&#x4E2D;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x7684;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF0C;&#x7136;&#x540E;&#x5C1D;&#x8BD5;&#x7A83;&#x53D6;&#x4EFB;&#x52A1;&#x3002;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x5C1D;&#x8BD5;&#x4ECE;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E0A;&#x7A83;&#x53D6;&#x4EFB;&#x52A1;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x4F1A;&#x4ECE;&#x4E0B;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5F00;&#x59CB;&#x904D;&#x5386;&#xFF0C;&#x901A;&#x8FC7;&#x81EA;&#x8EAB;&#x7684;&#x7EBF;&#x7A0B;&#x5E8F;&#x53F7;&#x6765;&#x786E;&#x5B9A;&#x5F00;&#x59CB;&#x904D;&#x5386;&#x7684;&#x7EBF;&#x7A0B;&#x5E8F;&#x53F7;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x6C60;&#x6709;&#x5F88;&#x591A;&#x597D;&#x5904;&#xFF0C;&#x8FD8;&#x6709;&#x5F88;&#x591A;&#x7684;&#x65B9;&#x5F0F;&#x80FD;&#x4E3A;&#x67D0;&#x4E9B;&#x7279;&#x6B8A;&#x7528;&#x6CD5;&#x63D0;&#x5347;&#x6027;&#x80FD;&#xFF0C;&#x4E0D;&#x8FC7;&#x8FD9;&#x5C31;&#x5F53;&#x505A;&#x7559;&#x7ED9;&#x8BFB;&#x8005;&#x7684;&#x4F5C;&#x4E1A;&#x5427;&#x3002;&#x7279;&#x522B;&#x662F;&#x8FD8;&#x6CA1;&#x6709;&#x63A2;&#x7A76;&#x52A8;&#x6001;&#x53D8;&#x6362;&#x5927;&#x5C0F;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;&#x5373;&#x4F7F;&#x7EBF;&#x7A0B;&#x963B;&#x585E;&#x65F6;(&#x4F8B;&#x5982;&#xFF1A;I/O&#x6216;&#x4E92;&#x65A5;&#x9501;)&#xFF0C;&#x7A0B;&#x5E8F;&#x90FD;&#x80FD;&#x4FDD;&#x8BC1;CPU&#x6700;&#x4F18;&#x7684;&#x4F7F;&#x7528;&#x7387;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;&#x7684;&#x9AD8;&#x7EA7;&#x7528;&#x6CD5;&#x2014;&#x2014;&#x4E2D;&#x65AD;&#x7EBF;&#x7A0B;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="9.0-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 第9章 高级线程管理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="9.2-chinese.html" class="navigation navigation-next " aria-label="Next page: 9.2 中断线程">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"9.1 线程池","level":"1.16.1","depth":2,"next":{"title":"9.2 中断线程","level":"1.16.2","depth":2,"path":"content/chapter9/9.2-chinese.md","ref":"content/chapter9/9.2-chinese.md","articles":[]},"previous":{"title":"第9章 高级线程管理","level":"1.16","depth":1,"path":"content/chapter9/9.0-chinese.md","ref":"content/chapter9/9.0-chinese.md","articles":[{"title":"9.1 线程池","level":"1.16.1","depth":2,"path":"content/chapter9/9.1-chinese.md","ref":"content/chapter9/9.1-chinese.md","articles":[]},{"title":"9.2 中断线程","level":"1.16.2","depth":2,"path":"content/chapter9/9.2-chinese.md","ref":"content/chapter9/9.2-chinese.md","articles":[]},{"title":"9.3 本章总结","level":"1.16.3","depth":2,"path":"content/chapter9/9.3-chinese.md","ref":"content/chapter9/9.3-chinese.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"plugins":["katex","ace"],"livereload":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"fontSettings":{"theme":"white","family":"msyh","size":2},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":10,"left":10,"top":10,"bottom":20}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"C++并发编程实战（第2版）","language":"zh-hans","gitbook":"*","description":"《C++ Concurrency In Action - SECOND EDITION》英文版的中文翻译版。","direction":"ltr"},"file":{"path":"content/chapter9/9.1-chinese.md","mtime":"2022-11-19T02:54:31.325Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-11-19T07:50:48.148Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

